package com.bckj.fastboot.paid.biz.application.consumer.pay;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.bckj.fastboot.mq.ons.OnsConsumeStrategy;
import com.bckj.fastboot.mq.ons.OnsListener;
import com.bckj.fastboot.paid.api.constant.PayMQConstants;
import com.bckj.fastboot.paid.biz.application.consumer.pay.strategy.PayOnsConsumeStrategyProvider;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@OnsListener(topic = PayMQConstants.PAY_TOPIC)
public class PayMsgConsumer implements MessageListener {


    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        String body = new String(message.getBody());
        log.info("支付消息内容:{}", body);
        OnsConsumeStrategy strategy = PayOnsConsumeStrategyProvider.getStrategy(message.getTag());
        if (strategy == null) {
            log.error("未找到对应的消费策略 tag=" + message.getTag());
            return Action.CommitMessage;
        }
        try {
            strategy.consume(body);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Action.ReconsumeLater;
        }
        return Action.CommitMessage;
    }
}
